﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BlackXS.Menu
{
    class ListMenu
    {
        List<String> entries = new List<String>();
        Int32 top;
        Int32 selectedIndex = -1;
        Boolean finished = false;

        public String[] Entries
        {
            get
            {
                return entries.ToArray();
            }
            set
            {
                entries.Clear();
                foreach (String s in value)
                {
                    entries.Add(s);
                }
            }
        }

        public Int32 AddEntry(String title)
        {
            entries.Add(title);
            return (entries.Count - 1);
        }
        public Int32 Show()
        {
            Console.CursorVisible = true;
            if (entries.Count > 0)
            {
                WriteMenu();
                MarkMenuEntry(0);

                do
                {
                    selectedIndex = Choose(Console.ReadKey(true).Key);
                } while (finished == false);

                Console.SetCursorPosition(0, top + entries.Count);

                return selectedIndex;
            }
            else
            {
                Console.Write("No entries are avaiable!\n");
                return -1;
            }
        }

        private void WriteMenu()
        {
            Int32 counter = 0;
            top = Console.CursorTop;
            foreach (String s in entries)
            {
                Console.Write("[ ] " + s + "\n");
                counter++;
            }
        }
        private Int32 Choose(ConsoleKey cky)
        {
            switch (cky)
            {
                case ConsoleKey.UpArrow:
                    if ((selectedIndex - 1) >= 0)
                    {
                        MarkMenuEntry(selectedIndex - 1);
                        return (selectedIndex);
                    }
                    else
                    {
                        MarkMenuEntry(0);
                        return 0;
                    }
                case ConsoleKey.DownArrow:
                    if ((selectedIndex + 1) <= (entries.Count - 1))
                    {
                        MarkMenuEntry(selectedIndex + 1);
                        return (selectedIndex);
                    }
                    else
                    {
                        MarkMenuEntry(entries.Count - 1);
                        return (entries.Count - 1);
                    }
                case ConsoleKey.Enter:
                    finished = true;
                    return selectedIndex;
                default:
                    return selectedIndex;
            }
        }
        private void MarkMenuEntry(Int32 index)
        {
            Console.SetCursorPosition(1, top + index);
            selectedIndex = index;
        }
    }
}
